package com.banmao.algorithm.No1To100.No98;

import java.util.ArrayList;
import java.util.List;

public class Solution1 {

    public boolean isValidBST(TreeNode root) {

        ArrayList<Integer> list = new ArrayList<Integer>();
        recursive(root, list);

        // 遍历判断list是否持续递增
        for (int i = 1; i < list.size(); i++) {
            Integer low = list.get(i - 1);
            Integer high = list.get(i);
            if(high <= low) {
                return false;
            }
        }

        return true;
    }

    private void recursive(TreeNode root, List list) {

        if(root == null) return;

        // 中序遍历，二叉搜索树的中序遍历是递增的
        recursive(root.left, list);

        list.add(root.val);

        recursive(root.right, list);

    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
